/*-+-*****************************************************************************
 Copyright (C) 2009-2012 Oceanside Software Corporation (R)
 Dallas, Texas

 Author       : Jason T. Brower
 Purpose      : This file is part of a point-of-sale software package
                originally written for Table-Service Restaurants.
              
 Date         : July 8th of 2012
              
 History      : The application that this software file belongs to was 
                written over a 3-Year period.  The first version was written
                in C# .Net 3.5 using Windows Forms.  Being new to .Net at
                the time, I soon learned of Windows Presentation Foundation,
                liked the graphics capabilities and databinding so I
                migrated to WPF.  When the .Net 4.0 framework was released
                the application was migrated from 3.5 to 4.0. Now today,
                .Net 4.5 RC is out and I was already in process of migrating
                before I made this public.
                
Excuse Me     : Please excuse the coding style, it is not representative of
                my typical work.  During the three years I was under a very
                tight budget, constantly running out of funding and was 
                working 18 hour days hyped up on 5-Hour Energy.  The 
                design is not textbook.  It is monolythic because of
                previous issues I will not get into.  There are few comments
                in the code and the ones that are there may be incorrect.
                
What it is not: This is not the exact version that I took through PA-DSS 2.0
                nor is it the exact version running at my customer sites.
                I removed some details for security purposes including some
                encryption algorithms.  The reporting was removed because
                I was including a third party library that cannot be 
                published with this Open Source.
                
What it is    : This application has been running at high volume restaraunts
                for over a year.  It never freezes, crashes or does anything
                unexpected.  So long as it is built, installed and configured
                correctly, it is a strong system that we can all enhance.

Contact       : Please note that I can no longer work for free.  However, you
                can reach me at information@oceansidesw.com.                                                      

*********************************************************************************/
using System;
using System.Xml;
using System.Xml.Linq;

#if _LOCALDB
  using Oceanside.LocalDB;
#else
using Oceanside.RemoteDB;
#endif
namespace Oceanside.Payments.Credit
{
    public class AuthOnly
    {
        static XmlDocument _template;

        static AuthOnly()
        {
            string template =
                                "<?xml version=\"1.0\"?>" +
                                "<TStream>" +
                                "<Transaction>" +
                                "<IpAddress></IpAddress>" +
                                "<IpPort></IpPort>" +
                                "<MerchantID></MerchantID>" +
                                ///"<TerminalID></TerminalID>" +
                                "<OperatorID></OperatorID>" +
                                "<Memo></Memo>" +
                                "<TranType></TranType>" +
                                "<Duplicate></Duplicate>" +
                                "<CardType></CardType>" +
                                "<TranCode></TranCode>" +
                                "<InvoiceNo></InvoiceNo>" +
                                "<RefNo></RefNo>" +
                                "<PartialAuth></PartialAuth>" +
                                "<Account>" +
                                "<Track2></Track2>" +
                                "<AcctNo></AcctNo>" +
                                "<ExpDate></ExpDate>" +
                                "</Account>" +
                                "<Amount>" +
                                "<Purchase></Purchase>" +
                                "<Authorize></Authorize>" +
                                "</Amount>" +
                                "<AVS>" +
                                "<Address></Address>" +
                                "<Zip></Zip>" +
                                "</AVS>" +
                                "<CVVData></CVVData>" +
                                "<SequenceNo></SequenceNo>" +
                                "<TerminalName></TerminalName>" +
                                "<ShiftID></ShiftID>" +
                                "<Signature></Signature>" +
                                "</Transaction>" +
                                "</TStream>";

            XDocument xd = XDocument.Parse(template);
            _template = new XmlDocument();
            _template.LoadXml(xd.ToString());
        }

        public string IpAddress;
        public string IpPort;
        public string InvoiceNo;
        public string Purchase;
        public string Authorize;
        public string Address;
        public string Zip;
        public string CVVData;
        public string SequenceNo;
        public string Signature;
        public string TraceData;

        public override string ToString()
        {
            if (_template != null)
            {
                return _template.InnerXml;
            }
            else
            {
                return "";
            }
        }

        public string Request(MagneticCard mc)
        {
            string response = null;

            if (mc.TrackTwo == null && (mc.Pan == null || mc.Expire == null))
            {
                throw new System.ArgumentNullException("Card Parameters contained Null Values");
            }
            else if (
                string.IsNullOrEmpty(InvoiceNo) ||
                string.IsNullOrEmpty(Authorize) ||
                string.IsNullOrEmpty(Purchase))
            {
                throw new System.ArgumentNullException("Function Parameters contained Null Values");
            }
            else
            {
                _template.GetElementsByTagName("IpAddress")[0].InnerText = IpAddress;
                _template.GetElementsByTagName("IpPort")[0].InnerText = IpPort;
                _template.GetElementsByTagName("MerchantID")[0].InnerText = PaymentEngine.MerchantId;
                _template.GetElementsByTagName("Memo")[0].InnerText = NotifyingSwRev.Swrev;
                //_template.GetElementsByTagName("TerminalID")[0].InnerText = PaymentEngine.GetTerminalId();
                _template.GetElementsByTagName("OperatorID")[0].InnerText = SHIFT.Current.User.Id.ToString();
                _template.GetElementsByTagName("TranType")[0].InnerText = "Credit";
                _template.GetElementsByTagName("Duplicate")[0].InnerText = PaymentEngine.AllowDuplicateOveride;
                _template.GetElementsByTagName("CardType")[0].InnerText = mc.CardType;
                _template.GetElementsByTagName("TranCode")[0].InnerText = "AuthOnly";
                _template.GetElementsByTagName("InvoiceNo")[0].InnerText = InvoiceNo;
                _template.GetElementsByTagName("RefNo")[0].InnerText = InvoiceNo;
                _template.GetElementsByTagName("Track2")[0].InnerText = mc.TrackTwo;
                ///CAUTION, the template used for all transactions is static.  Anytime that you set a value based
                ///on an IF statement, assure that there is a corresponding else statement to set the value to null
                ///or a value left over from a previous transaction could be USED!
                if (mc.TrackTwo == null)
                {
                    _template.GetElementsByTagName("AcctNo")[0].InnerText = mc.Pan;
                    _template.GetElementsByTagName("ExpDate")[0].InnerText = mc.Expire;
                }
                else
                {
                    _template.GetElementsByTagName("AcctNo")[0].InnerText = null;
                    _template.GetElementsByTagName("ExpDate")[0].InnerText = null;
                }
                _template.GetElementsByTagName("Purchase")[0].InnerText = Purchase;
                _template.GetElementsByTagName("Authorize")[0].InnerText = Authorize;
                _template.GetElementsByTagName("Address")[0].InnerText = Address;
                _template.GetElementsByTagName("Zip")[0].InnerText = Zip;
                _template.GetElementsByTagName("CVVData")[0].InnerText = CVVData;
                _template.GetElementsByTagName("SequenceNo")[0].InnerText = SequenceNo;
                _template.GetElementsByTagName("TerminalName")[0].InnerText = KIOSK.Get(SHIFT.GlobalContext).Name;
                _template.GetElementsByTagName("ShiftID")[0].InnerText = DAYPART.GetCurrDaypart(SHIFT.GlobalContext).Name;
                _template.GetElementsByTagName("Signature")[0].InnerText = Signature;

                response =
                    PaymentEngine.CreditComChan.ProcessTransaction(_template.InnerXml, PaymentEngine.CONST_NO_DIALOG, null, TraceData);
            }

            return response;
        }
    }
}
